<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 成员列表组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Users_Ucenter extends Widget_Abstract_Users
{
	
    /**
     * 内容个数
     *
     * @access private
     * @var integer
     */
    protected $_total = false;

    /**
     * 当前页
     *
     * @access private
     * @var integer
     */
    protected $_currentPage = 1;
	
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
		$this->user->pass('admin');
		
        $this->parameter->setDefault('pageSize=20');
        $this->_currentPage = $this->request->get('page', 1);

        /** 构建基础查询 */
        $select = $this->select();
		
		/** 查询 */
        if (NULL != ($keywords = $this->request->filter('search')->keywords)) {
			$search = in_array($this->request->search, array('uid', 'name', 'mail', 'group', 'ip')) ? 
			$this->request->search : 'name';
			'ip' == $search && $keywords = ip2long($keywords);
			$select->where("table.users.{$search} LIKE ?", '%' . $keywords . '%');
        }

		/** 计算数目 */
		$this->_total = (clone $select)
            ->select(array('COUNT(*)' => 'num'))
            ->fetchObject()->num;

        $select->order('table.users.uid', 'ASC')
        ->page($this->_currentPage, $this->parameter->pageSize);

        $select->fetchAll(array($this, 'push'));
    }

    /**
     * 输出分页
     *
     * @access public
     * @return void
     */
    public function pageNav()
    {
        $query = $this->request->makeUriByRequest('page={page}');

        /** 使用盒状分页 */
        $nav = new Page_Box($this->_total,
        $this->_currentPage, $this->parameter->pageSize, $query);
        return $nav->render('&laquo;', '&raquo;');
    }
	
    public function userList()
    {
        $html = '<form method="get"><div class="cola-search">
		<select name="search">
			<option value="uid"'.('uid' == $this->request->search ? ' selected="true"' : '') .'>用户 ID</option>
			<option value="name"'.((null === $this->request->search || 'name' == $this->request->search) ? ' selected="true"' : '') .'>用户名</option>
			<option value="mail"'.('mail' == $this->request->search ? ' selected="true"' : '') .'>Email</option>
			<option value="group"'.('group' == $this->request->search ? ' selected="true"' : '') .'>group</option>
			<option value="ip"'.('ip' == $this->request->search ? ' selected="true"' : '') .'>创建 IP</option>
		</select>
        <input type="text" placeholder="'._t('请输入关键字').'" value="'. htmlspecialchars($this->request->keywords).'" name="keywords" /> 
		';
        if ('' != $this->request->keywords || '' != $this->request->uid){
            $html .= '<a class="cola-search-cancel ci-cancel" href="'.$this->options->ucenterUrl('users', false).'"></a>';
	    }
        $html .= '</div></form>';
		
        if ($this->have()) {
            $WidgetMenu = $this->widget('Widget_Menu');
			$Layout = new Layout();
			$Layout->header(array(
				$this->widget('Widget_Users_Edit')->operate(),
			    _t('用户名'),
				_t('昵称'),
			    _t('电子邮件'),
				_t('用户组'),
			));
			while ($this->next()){
			    $Layout->lists(array(
				    '<a class="cola-balloon" href="'.$this->options->ucenterUrl('posts?uid=' . $this->uid, false).'">'.$this->contentsNum.'</a>&nbsp;&nbsp;<a href="' .$this->options->ucenterUrl('user?uid=' . $this->uid, false).'">'.$this->name.'</a>&nbsp;&nbsp;<a class="text-muted text-md" href="'.$this->permalink.'"><i class="ci-link"></i></a>',
					'<span class="text-muted text-md">' . $this->nickName . '</span>',
					$this->mail ? '<a href="mailto:'.$this->mail.'">'.$this->mail.'</a>' : _t('暂无'),
					'<span class="text-muted text-md">' . $this->user->groups[0][$this->group] . '</span>'
				), array('data-uid'=>$this->uid));
			}
			$Layout->append('<ul class="cola-pager">' . $this->pageNav() . '</ul>');
			$html .= $Layout->render();

		}
		return $html;
	}
	
	
	
	
}
